package com.tencent.shadow.sample.host

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.tencent.shadow.dynamic.host.EnterCallback
import com.tencent.shadow.sample.host.Constant.PART_KEY_PLUGIN_BASE
import com.tencent.shadow.sample.host.databinding.LayoutTestBinding

class MainActivity : AppCompatActivity() {

    lateinit var binding: LayoutTestBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = LayoutTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.tvText.setOnClickListener {
            startPlugin()
        }
    }

    fun startPlugin() {
        PluginHelper.getInstance().singlePool.execute {
            HostApplication.getApp().loadPluginManager(PluginHelper.getInstance().pluginManagerFile)
            val bundle = Bundle()
            bundle.putString(Constant.KEY_PLUGIN_ZIP_PATH, PluginHelper.getInstance().pluginZipFile.absolutePath)
            bundle.putString(Constant.KEY_PLUGIN_PART_KEY, PART_KEY_PLUGIN_BASE)
            bundle.putString(Constant.KEY_ACTIVITY_CLASSNAME, "com.tencent.shadow.sample.plugin.app.lib.gallery.splash.SplashActivity")
            HostApplication.getApp().pluginManager
                .enter(this, Constant.FROM_ID_START_ACTIVITY.toLong(), bundle, object : EnterCallback {
                    override fun onShowLoadingView(view: View) {

                        //binding.layout.post {
                        //    binding.layout.addView((view))
                        //}

                    }

                    override fun onCloseLoadingView() {
                        finish()
                    }

                    override fun onEnterComplete() {}
                })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        HostApplication.getApp().pluginManager.enter(this, Constant.FROM_ID_CLOSE.toLong(), null, null)
    }
}